<?php

/**
 * This is the model class for table "tbl_equipment".
 *
 * The followings are the available columns in table 'tbl_equipment':
 * @property integer $id
 * @property string $custno
 * @property string $locno
 * @property string $mfg
 * @property string $model
 * @property string $serial
 * @property string $install
 * @property string $removed
 * @property string $warranty
 * @property string $seragrno
 * @property string $equiploc
 * @property string $notes
 * @property string $invoice
 * @property string $eqtype
 * @property string $plan
 * @property integer $status
 * @property string $image
 * @property string $description
 * @property integer $pmrequired
 * @property decimal $qty
 * @property string $ncr_billed_serial
 */
class Equipment extends CActiveRecord
{
    const PM_YES = 1;
    const PM_NO = 0;
    public $file;

    /**
     * Returns the static model of the specified AR class.
     * @return Equipment the static model class
     */
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'tbl_equipment';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('custno, locno', 'required'),
            array('status, pmrequired', 'numerical', 'integerOnly' => true),
            array('custno', 'length', 'max' => 7),
            array('locno', 'length', 'max' => 5),
            array('mfg', 'length', 'max' => 20),
            array('model', 'length', 'max' => 30),
            array('serial', 'length', 'max' => 50),
            array('seragrno, invoice', 'length', 'max' => 10),
            array('equiploc, plan, image,terminalid', 'length', 'max' => 150),
            array('eqtype', 'length', 'max' => 25),
            array('ncr_billed_serial', 'length', 'max' => 150),
            array('install, removed, warranty, notes,qty,description,sl', 'safe'),
            array('file', 'file', 'types' => 'bmp,jpg,jpeg,gif,png', 'maxSize' => 10485760, 'allowEmpty' => true),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, custno, locno, mfg, model, serial, install, removed, warranty, seragrno, equiploc, notes, invoice, eqtype, plan, status, image, pmrequired', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'custno' => 'Customer number',
            'locno' => 'Location number',
            'mfg' => 'Manufacturer',
            'model' => 'Model',
            'serial' => 'Serial',
            'install' => 'Install',
            'removed' => 'Removed',
            'warranty' => 'Warranty',
            'seragrno' => 'Contract ID',
            'equiploc' => 'Equipment Location',
            'notes' => 'Notes',
            'invoice' => 'Invoice',
            'eqtype' => 'Equipment Type',
            'plan' => 'Plan',
            'status' => 'Status',
            'image' => 'Image',
            'pmrequired' => 'PM Required',
	    'sl' => ' Service Level',
	    'terminalid'=>'Terminal ID' ,
	    'ncr_billed_serial'=>'NCR Billed Serial'
        );
    }

    public function getListByLocation($custno = NULL, $locno = NULL)
    {
        if ($custno == NULL || $locno == NULL) {
            $custno = $this->custno;
            $locno = $this->locno;
        }
        $list = $this->findAll('custno=:custno AND locno=:locno', array('custno' => $custno, 'locno' => $locno));
        $rs = array();
        if (count($list) > 0) {
            foreach ($list as $item) {
                $rs[$item->id] = $item->mfg . ' ' . $item->model . ' ' . $item->serial;
            }
        }
        return $rs;
    }

    public function beforeSave()
    {


        $this->file = CUploadedFile::getInstance($this, 'file');
        if ($this->file != NULL) {
            //made a random name base on timestamp
            $ext = getFileExt($this->file);
            $name = getFileBodyName($this->file);


            $this->image = $name . '_' . time() . $ext;

            $this->file->saveAs('images/content/' . $this->image, true);
        }
        return parent::beforeSave();


    }

    public function beforeDelete()
    {
        parent::beforeDelete();
        if (strlen(trim($this->image)) > 0 && file_exists('images/content/' . $this->image)) {
            unlink('images/content/' . $this->image);
        }
        return true;
    }

    public function getPmrequiredText()
    {
        return isset($this->PmrequiredList[$this->pmrequired]) ? $this->PmrequiredList[$this->pmrequired] : NULL;
    }


    public function getPmrequiredList()
    {
        return array(
            self::PM_NO => 'No',
            self::PM_YES => 'Yes',
        );
    }

    public function getStatusText()
    {
        return isset($this->statusList[$this->status]) ? $this->statusList[$this->status] : NULL;
    }

    public function getStatusList()
    {
        return array(
            '-1' => 'Pending',
            '0' => 'Active',
            '1' => 'Removed',
        );
    }

    public function getPlanTypeText()
    {
        return isset($this->planTypeList[$this->plan]) ? $this->planTypeList[$this->plan] : NULL;
    }

    public function getPlanTypeList()
    {
        $data = Yii::app()->db->createCommand('select * from tbl_plantypes')->queryAll();
        if (count($data) > 0) {
            $rs = array();
            foreach ($data as $i => $item) {
                $rs[$item['id']] = $item['plantype'];
            }
            return $rs;
        }
        return NULL;
    }
}
